home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 295_01 / bsetvbuf.c < prev    next >
Text File  |  1989-12-28  |  3KB  |  124 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bsetvbuf.c    1.2 - 89/10/31" */
  5.  
  6. #include <errno.h>
  7. /*#include <stddef.h>*/
  8. #include "blkio_.h"
  9.  
  10. /*man---------------------------------------------------------------------------
  11. NAME
  12.      bsetvbuf - assign buffering to a block file
  13.  
  14. SYNOPSIS
  15.      #include <blkio.h>
  16.  
  17.      int bsetvbuf(bp, buf, blksize, bufcnt)
  18.      BLKFILE *bp;
  19.      void *buf;
  20.      size_t blksize;
  21.      size_t bufcnt;
  22.  
  23. DESCRIPTION
  24.      The bsetvbuf function causes the block size and buffer block
  25.      count of the block file associated with BLKFILE pointer bp to be
  26.      changed to bufcnt and blksize, with the same effects as if the
  27.      file had been opened with these values.  If bufcnt has a value of
  28.      zero, the file will be completely unbuffered.  If buf is not the
  29.      NULL pointer, the storage area it points to will be used instead
  30.      of one automatically allocated for buffering.  In this case, buf
  31.      must point to a storage area of size no less than
  32.  
  33.           header size + block size * buffer count
  34.  
  35.      bsetvbuf may be called at any time after opening the block file,
  36.      before and after it is read or written.
  37.  
  38.      bsetvbuf will fail if one or more of the following is true:
  39.  
  40.      [EINVAL]       bp is not a valid BLKFILE pointer.
  41.      [EINVAL]       blksize is less than 1.
  42.      [ENOMEM]       Enough memory is not available for the
  43.                     calling process to allocate.
  44.      [BENOPEN]      bp is not open.
  45.  
  46. SEE ALSO
  47.      bopen, bsetbuf.
  48.  
  49. DIAGNOSTICS
  50.      Upon successful completion, a value of 0 is returned.  Otherwise,
  51.      a value of -1 is returned, and errno set to indicate the error.
  52.  
  53. ------------------------------------------------------------------------------*/
  54. int bsetvbuf(bp, buf, blksize, bufcnt)
  55. BLKFILE *bp;
  56. void *buf;
  57. size_t blksize;
  58. size_t bufcnt;
  59. {
  60.     /* validate arguments */
  61.     if (!b_valid(bp) || (blksize == 0)) {
  62.         errno = EINVAL;
  63.         return -1;
  64.     }
  65.  
  66.     /* check if not open */
  67.     if (!(bp->flags & BIOOPEN)) {
  68.         errno = BENOPEN;
  69.         return -1;
  70.     }
  71.  
  72.     /* synchronize file with buffers */
  73.     if (bsync(bp) == -1) {
  74.         BEPRINT;
  75.         return -1;
  76.     }
  77.  
  78.     /* free old buffer storage */
  79.     b_free(bp);
  80.  
  81.     /* modify BLKFILE control structure contents */
  82.     bp->flags &= ~BIOUSRBUF;
  83.     bp->blksize = blksize;
  84.     bp->bufcnt = bufcnt;
  85.     bp->endblk = 0;
  86.     bp->most = 0;
  87.     bp->least = 0;
  88.     if (b_uendblk(bp, &bp->endblk) == -1) {
  89.         BEPRINT;
  90.         return -1;
  91.     }
  92.  
  93.     /* check if not buffered */
  94.     if (bp->bufcnt == 0) {
  95.         errno = 0;
  96.         return 0;
  97.     }
  98.  
  99.     /* set user supplied buffer flag */
  100.     if (buf != NULL) {
  101.         bp->flags |= BIOUSRBUF;
  102.     }
  103.  
  104.     /* allocate memory for bp */
  105.     if (b_alloc(bp) == -1) {
  106.         BEPRINT;
  107.         return -1;
  108.     }
  109.  
  110.     /* connect user supplied buffer */
  111.     if (bp->flags & BIOUSRBUF) {
  112.         bp->blkbuf = buf;
  113.     }
  114.  
  115.     /* initialize linked list of buffers */
  116.     if (b_initlist(bp) == -1) {
  117.         BEPRINT;
  118.         return -1;
  119.     }
  120.  
  121.     errno = 0;
  122.     return 0;
  123. }
  124.